<?php

namespace backend\controllers;
use yii;
use yii\data\Pagination;
use backend\models\Lunbo;
use yii\web\UploadedFile;

header('content-type:text/html;charset=utf-8');
class LunboController extends \yii\web\Controller
{
	public $enableCsrfValidation = false;
    public function actionAdd()
    {
    	if(yii::$app->request->isPost)
    	{
    		//print_r($_FILES);die;
    		//$upload=new UploadedFile(); //实例化上传类
			//$name=$upload->getInstanceByName('image'); //获取文件原名称
			//echo $name;die;
			//$img=$_FILES['image']; //获取上传文件参数
			//var_dump($img);die;
			//$upload->tempName=$img['name']; //设置上传的文件的临时名称
			//$img_path='uploads/'.$name; //设置上传文件的路径名称(这里的数据进行入库)
			move_uploaded_file($_FILES["image"]["tmp_name"],"../../../public/lunbo/".$_FILES["image"]["name"]);
			//$arr=$upload->saveAs($img_path); //保存文件
			//var_dump($arr);die;
			$image = $_FILES['image']['name'];
			//echo $image;die;
			$test=new Lunbo();
			$test->image=$image;
			$test->name=$_POST['name'];
			$test->order=$_POST['order'];
			$test->is_show=$_POST['is_show'];
			$test->link=$_POST['link'];
			$test->save();

			$this->redirect("index.php?r=lunbo/list");
    	}
        return $this->render('add');
    }
    public function actionList()
    {
    	$test=new Lunbo();	//实例化model模型
		$arr=$test->find();
		//$countQuery = clone $arr;
		$pages = new Pagination([
			//'totalCount' => $countQuery->count(),
			'totalCount' => $arr->count(),
			'pageSize'   => 5   //每页显示条数
		]);
		$models = $arr->offset($pages->offset)
			->limit($pages->limit)
			->all();
		//print_r($models);die;
		return $this->render('list', [
			'models' => $models,
			'pages'  => $pages
		]);
    }
}
